package org.jeecg.modules.wechat.utils;

import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

/**
 * HTTP客户端工具类
 */
public class HttpClientUtil {
    
    private static final int CONNECT_TIMEOUT = 5000;
    private static final int SOCKET_TIMEOUT = 5000;
    private static final int REQUEST_TIMEOUT = 5000;
    
    /**
     * 发送GET请求
     *
     * @param url 请求地址
     * @return 响应内容
     * @throws IOException IO异常
     */
    public static String get(String url) throws IOException {
        CloseableHttpClient httpClient = null;
        CloseableHttpResponse httpResponse = null;
        try {
            httpClient = HttpClients.createDefault();
            
            RequestConfig requestConfig = RequestConfig.custom()
                    .setConnectTimeout(CONNECT_TIMEOUT)
                    .setSocketTimeout(SOCKET_TIMEOUT)
                    .setConnectionRequestTimeout(REQUEST_TIMEOUT)
                    .build();
            
            HttpGet httpGet = new HttpGet(url);
            httpGet.setConfig(requestConfig);
            
            httpResponse = httpClient.execute(httpGet);
            return EntityUtils.toString(httpResponse.getEntity(), "UTF-8");
        } finally {
            closeResources(httpClient, httpResponse);
        }
    }
    
    /**
     * 发送POST请求
     *
     * @param url  请求地址
     * @param body 请求体内容
     * @return 响应内容
     * @throws IOException IO异常
     */
    public static String post(String url, String body) throws IOException {
        CloseableHttpClient httpClient = null;
        CloseableHttpResponse httpResponse = null;
        try {
            httpClient = HttpClients.createDefault();
            
            RequestConfig requestConfig = RequestConfig.custom()
                    .setConnectTimeout(CONNECT_TIMEOUT)
                    .setSocketTimeout(SOCKET_TIMEOUT)
                    .setConnectionRequestTimeout(REQUEST_TIMEOUT)
                    .build();
            
            HttpPost httpPost = new HttpPost(url);
            httpPost.setConfig(requestConfig);
            httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
            
            if (body != null && !body.isEmpty()) {
                StringEntity entity = new StringEntity(body, "UTF-8");
                httpPost.setEntity(entity);
            }
            
            httpResponse = httpClient.execute(httpPost);
            return EntityUtils.toString(httpResponse.getEntity(), "UTF-8");
        } finally {
            closeResources(httpClient, httpResponse);
        }
    }
    
    /**
     * 关闭资源
     *
     * @param httpClient   HTTP客户端
     * @param httpResponse HTTP响应
     */
    private static void closeResources(CloseableHttpClient httpClient, CloseableHttpResponse httpResponse) {
        try {
            if (httpResponse != null) {
                httpResponse.close();
            }
            if (httpClient != null) {
                httpClient.close();
            }
        } catch (IOException e) {
            // 忽略关闭异常
        }
    }
}